package com.dsp.web;


import java.io.ByteArrayOutputStream;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.BaseUser;
import com.core.util.DspUtil;
import com.dsp.bean.Calc;
import com.dsp.bean.Jobs;
import com.dsp.bean.Message;
import com.dsp.services.CalcService;
import com.dsp.services.JobsService;

@Controller("CalcsController")
public class CalcsController extends DspController<Calc>{

	public CalcsController() {
		super(Calc.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);
	@Autowired
	private CalcService calcService;

	@Autowired
	private JobsService jobsService;

	@RequestMapping(value = "/calcs", method = RequestMethod.GET)
	public ModelAndView list() throws IOException, ClassNotFoundException, SQLException {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("calc/list");
		mav.addObject("path", "calc/list.jsp");
		mav.addObject("current", "calcs");
		return mav;
	}

	@RequestMapping(value = "/calcs_c", method = RequestMethod.POST)
	@ResponseBody
	public Message calcaulte(HttpServletRequest request, HttpServletResponse response) {
		 Message message = new Message();
		SecurityContextImpl upat = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		BaseUser bu = (BaseUser) upat.getAuthentication().getPrincipal();
		Jobs job = new Jobs();
		job.setJobsType("calc");
		job.setUserId(bu.getId());
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
		Calc calc = new Calc();
		String calcYearMonth = (String)request.getParameter("calcYearMonth");
		String[] companys = null;
		String tmpCompany = request.getParameter("company");
		if(tmpCompany == null) {
			 companys = request.getParameterValues("company[]");
		} else {
			companys = tmpCompany.split(" ");
		}
		try {
			if (companys != null) {
			List<Long> l = Arrays.asList(DspUtil.arrayToLong(companys));
			calc.setCompany(l);
			calc.setCalcYearMonth(calcYearMonth);
			oos = new ObjectOutputStream(byteOut);
			oos.writeObject(calc);
			job.setParasOne(Hibernate.createBlob(byteOut.toByteArray()));
			this.jobsService.save(job);
			/**
			 * Jobs job = this.jobsService.findById(Long.valueOf(2));
		if (job != null) {
			InputStream is = job.getParasOne().getBinaryStream();
			ObjectInputStream ois = new ObjectInputStream(is);
			Calc calc = (Calc)ois.readObject();
			logger.debug(calc);
		}
			 */
			} else {
				message.setSuccess(false);
				List<String> msg = new ArrayList<String>();
				msg.add("必须选择公司,才能排入计算队列");
				message.setMsg(msg);
			}
		} catch (IOException e) {
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(e.getMessage());
			message.setMsg(msg);
			e.printStackTrace();
		} catch (Exception se) {
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getMessage());
			message.setMsg(msg);
			se.printStackTrace();
		} 
		 return message;
	}

	/**
	 * @return the calcService
	 */
	public CalcService getCalcService() {
		return calcService;
	}

	/**
	 * @param calcService
	 *            the calcService to set
	 */
	public void setCalcService(CalcService calcService) {
		this.calcService = calcService;
	}

	/**
	 * @return the jobsService
	 */
	public JobsService getJobsService() {
		return jobsService;
	}

	/**
	 * @param jobsService
	 *            the jobsService to set
	 */
	public void setJobsService(JobsService jobsService) {
		this.jobsService = jobsService;
	}

}
